<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>takePhoto demo</title>
</head>
<body>
    <div>
        <h2>takePhoto demo(手机拍照)</h2>
        <hr>
        <div>
            <input type="file" accept="image/*" capture="camera" onchange="takePhoto(event, 'photo')" />
            <br>
            <img id="photo" src="" style="width: 200px;" alt="暂无图像" />
        </div>
    </div>
    <script>
        function takePhoto(e, showImgId) {
            console.log('takePhoto-e:', e);
            const input = e.target;
            if(input.files && input.files.length > 0) {
                const file = input.files[0];
                const url = URL.createObjectURL(file);
                const img = document.getElementById(showImgId);
                img.src = url;
                img.onload = function() {
                    URL.revokeObjectURL(url);
                };
            }
        }
    </script>
</body>
</html>